package com.easylinkin.sm.controller;

import com.easylinkin.sm.constant.LogConstant;
import com.easylinkin.sm.entity.Role;
import com.easylinkin.sm.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.boot.data.DisplayableControllerSupport;
import site.morn.log.OperateGroup;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * 角色控制器
 *
 * @author majintao
 * @since 1.0.0, 2019-08-19-9:37
 */
@Api(tags = "角色管理")
@OperateGroup(value = "role", args = LogConstant.Module.ROLE)
@RestController()
@RequestMapping("role")
public class RoleController extends DisplayableControllerSupport<Role, Long, RoleService> {

  @ApiOperation(value = "根据用户Id查询用户")
  @ApiImplicitParam(name = "userId", value = "用户Id", required = true)
  @GetMapping("getRolesByUserId/{userId}")
  public RestMessage getRolesByUserId(@PathVariable Long userId) {
    return RestBuilders.successMessage(service().getByUserId(userId));
  }

  @ApiOperation(value = "查询当前用户可见的角色列表")
  @GetMapping("searchAll")
  public RestMessage getRolesForCurrentUser(String departmentId) {
    Long deptId = -1L;
    if (!StringUtils.isEmpty(departmentId)){
      deptId = Long.valueOf(departmentId);
    }
    return RestBuilders.successMessage(service().getRolesForCurrentUser(deptId));
  }

  @ApiOperation(value = "查询当前选择机构下的角色列表")
  @GetMapping("searchRoles")
  public RestMessage getRolesByDepartmentId(String departmentId){
    Long deptId = -1L;
    if (!StringUtils.isEmpty(departmentId)){
      deptId = Long.valueOf(departmentId);
    }
    return RestBuilders.successMessage(service().getRolesByDepartmentId(deptId));
  }
}
